package com.zp.business.impl.lab;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.lab.LabApply;
import com.zp.business.entity.lab.LabItem;
import com.zp.business.entity.lab.bo.LabItemBo;
import com.zp.business.entity.lab.bo.SaveLabItemBo;
import com.zp.business.entity.lab.vo.LabItemVo;
import com.zp.business.mapper.lab.LabItemMapper;
import com.zp.business.service.lab.ILabItemService;
import com.zp.common.core.constant.BaseConstants;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 检验申请子表Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-03-05
 */
@RequiredArgsConstructor
@Service
public class LabItemServiceImpl implements ILabItemService {

    private final LabItemMapper baseMapper;

    /**
     * 查询检验申请子表
     */
    @Override
    public LabItemVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询检验申请子表列表(分页)
     */
    @Override
    public TableDataInfo<LabItemVo> queryPageList (LabItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LabItem> lqw = buildQueryWrapper(bo);
        Page<LabItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询检验申请子表列表(不分页)
     */
    @Override
    public List<LabItemVo> queryList (LabItemBo bo) {
        LambdaQueryWrapper<LabItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LabItem> buildQueryWrapper (LabItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LabItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getApplyId()), LabItem::getApplyId, bo.getApplyId());
        lqw.eq(StringUtils.isNotBlank(bo.getClinicItemId()), LabItem::getClinicItemId, bo.getClinicItemId());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), LabItem::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getItemType()), LabItem::getItemType, bo.getItemType());
        lqw.eq(StringUtils.isNotBlank(bo.getItemCode()), LabItem::getItemCode, bo.getItemCode());
        lqw.eq(bo.getCosts() != null, LabItem::getCosts, bo.getCosts());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), LabItem::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), LabItem::getRemarks, bo.getRemarks());
        lqw.eq(bo.getRealDate() != null, LabItem::getRealDate, bo.getRealDate());
        return lqw;
    }

    /**
     * 新增检验申请子表
     */
    @Override
    public Boolean insertByBo (LabItemBo bo) {
        LabItem add = BeanUtil.toBean(bo, LabItem.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改检验申请子表
     */
    @Override
    public Boolean updateByBo (LabItemBo bo) {
        LabItem update = BeanUtil.toBean(bo, LabItem.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (LabItem entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检验申请子表
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<LabItem> initLabItemAndSave (LabApply labApply, List<SaveLabItemBo> saveLabItemBo) {
        List<LabItem> labItemList = new ArrayList<>();
        saveLabItemBo.forEach(item -> {
            LabItem labItem = new LabItem();
            labItem.initEntity();
            labItem.setApplyId(labApply.getId());
            labItem.setOrgId(labApply.getOrgId());
            labItem.setRealDate(labApply.getRealDate());
            labItem.setClinicItemId(item.getClinicId());
            labItem.setItemName(item.getItemName());
            labItem.setItemType(item.getLabId());
            labItem.setItemCode(item.getItemCode());
            labItem.setNum(item.getNum());
            labItem.setPrice(item.getPrice());
            labItem.setChargeFlag(labApply.getChargeFlag());
            // 设置确认类型： 未确认 1
            labItem.setConfirmFlag(BaseConstants.CONFIRM_FLAG.UNISSUED);
            labItem.setCosts(item.getPrice().multiply(new BigDecimal(item.getNum())));
            labItem.setSpecimen(item.getSpecimen() + "," + item.getSpecimenName());
            labItem.setLabTube(item.getLabTube() + "," + item.getLabTubeName());
            labItemList.add(labItem);
        });
        baseMapper.insertBatch(labItemList);
        return labItemList;
    }

    @Override
    public Boolean deleteByBo (String labApplyId) {
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("apply_id", labApplyId);
        return baseMapper.deleteByMap(columnMap) > 0;
    }
}
